package com.ywxx.dyhelper.activity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;

import com.ywxx.dyhelper.BuildConfig;
import com.ywxx.dyhelper.R;
import com.ywxx.dyhelper.base.BaseAcitivity;
import com.ywxx.dyhelper.model.TaskConfig;
import com.ywxx.dyhelper.utils.ConstValues;
import com.ywxx.dyhelper.utils.SqliteDBAcces;
import com.ywxx.dyhelper.widget.NumberAddSubView;

/**
 * 一键广告
 */
public class AutoAdvertisementActivity extends BaseAcitivity
{

    private NumberAddSubView labNum;
    private NumberAddSubView labOperNum;
    private NumberAddSubView labEmotIndex;
    private Switch sw_all;
    private Switch sw_hello;
    private Switch sw_emoticon;
    private Switch sw_text;
    private EditText tbInput;
    private Button btnClear;

    LinearLayout llStart;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_advertisement);

        InitUI();
        DataToUI();

    }

    @Override
    protected void InitUI()
    {
        InitTitle();
        labNum = findViewById(R.id.labNum);
        labOperNum = findViewById(R.id.labOperNum);
        labEmotIndex = findViewById(R.id.labEmotIndex);
        sw_all = findViewById(R.id.sw_all);
        sw_hello = findViewById(R.id.sw_hello);
        sw_emoticon = findViewById(R.id.sw_emoticon);
        sw_text = findViewById(R.id.sw_text);
        tbInput = findViewById(R.id.tbInput);
        llStart = findViewById(R.id.llStart);
        btnClear = findViewById(R.id.btnClear);

        labNum.setValue(100);
        labNum.setSeekNum(10);
        labNum.setMaxValue(10000);


        labOperNum.setValue(1000);
        labOperNum.setSeekNum(100);
        labOperNum.setMaxValue(60000);

        labEmotIndex.setMaxValue(1);
        labEmotIndex.setMaxValue(8);

        llStart.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                appconfig = new TaskConfig();
                appconfig.runtime = labNum.getValue();
                appconfig.oprtime = labOperNum.getValue();
                appconfig.sendall = sw_all.isChecked();
                appconfig.sayhello = sw_hello.isChecked();
                appconfig.sendemot = sw_emoticon.isChecked();
                appconfig.like_num = labEmotIndex.getValue();
                appconfig.sendmessage = sw_text.isChecked();
                appconfig.unit = "条";
                appconfig.msg = tbInput.getText().toString();

                ConstValues.SaveValue(AutoAdvertisementActivity.this, "runtime" + TaskType, appconfig.runtime);
                ConstValues.SaveValue(AutoAdvertisementActivity.this, "opertime" + TaskType, appconfig.oprtime);
                ConstValues.SaveValue(AutoAdvertisementActivity.this, "sendall" + TaskType, appconfig.sendall);
                ConstValues.SaveValue(AutoAdvertisementActivity.this, "sayhello" + TaskType, appconfig.sayhello);
                ConstValues.SaveValue(AutoAdvertisementActivity.this, "emoticon" + TaskType, appconfig.sendemot);
                ConstValues.SaveValue(AutoAdvertisementActivity.this, "text" + TaskType, appconfig.sendmessage);
                ConstValues.SaveValue(AutoAdvertisementActivity.this, "message" + TaskType, tbInput.getText().toString());
                ConstValues.SaveValue(AutoAdvertisementActivity.this, "emoticon_index" + TaskType, appconfig.like_num);

                StartTaskServer(TaskType);
            }
        });


        sw_emoticon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b)
            {
                findViewById(R.id.llemoticon).setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });

        sw_text.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b)
            {
                findViewById(R.id.tbInput).setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });

        btnClear.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                DropTable();
            }


        });
    }

    @Override
    protected void DataToUI()
    {
        int runtime = ConstValues.getIntValue(this, "runtime" + TaskType, BuildConfig.DEBUG ? 30 : 100);
        int opertime = ConstValues.getIntValue(this, "opertime" + TaskType, 1000);
        boolean sendall = ConstValues.getBooleanValue(this, "sendall" + TaskType, false);
        boolean sayhello = ConstValues.getBooleanValue(this, "sayhello" + TaskType, true);
        boolean emoticon = ConstValues.getBooleanValue(this, "emoticon" + TaskType, false);
        int emoticon_index = ConstValues.getIntValue(this, "emoticon_index" + TaskType, 1);
        boolean text = ConstValues.getBooleanValue(this, "text" + TaskType, false);
        String message = ConstValues.getValue(this, "message" + TaskType, "");
        labNum.setValue(runtime);
        labOperNum.setValue(opertime);
        labEmotIndex.setValue(emoticon_index);
        sw_all.setChecked(sendall);
        sw_hello.setChecked(sayhello);
        sw_emoticon.setChecked(emoticon);
        sw_text.setChecked(text);
        tbInput.setText(message);

    }

    private void DropTable()
    {
        SQLiteDatabase db = openOrCreateDatabase(ConstValues.DBFilePath, Context.MODE_PRIVATE, null);
        if (db == null)
        {
            Toast.makeText(this, "异常1", Toast.LENGTH_SHORT).show();
        }

        if (db != null)
        {
            SqliteDBAcces DBAccess = new SqliteDBAcces(db);
            DBAccess.DropTable("hy_auto_adv_log");
            DBAccess.CreateAutoAdv();
        }

    }
}